<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <div class="input-search">
        <el-input
          clearable
          @clear="getOrderlist"
          placeholder="请输入内容"
          class="input-with-select"
          v-model="info.query"
        >
          <el-button slot="append" icon="el-icon-search" @click="getOrderlist"></el-button>
        </el-input>
      </div>
      <!-- table表格区域 -->
      <el-table :data="orderList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="445"></el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="80"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="80">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.pay_status=='0'?'danger':'success'"
            >{{scope.row.pay_status=='0'?'未付款':'已付款'}}</el-tag>
            <!-- <el-tag type="success" v-else>已付款</el-tag> -->
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="80"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">{{scope.row.create_time|dataFormat}}</template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="showLocationDialog"></el-button>
          <el-button size="mini" type="success" icon="el-icon-location" @click="showProcessDialog"></el-button>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="info.pagenum"
        :page-sizes="[5, 8, 10, 20]"
        :page-size="info.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="editLocationDialog" width="50%" @close="dialogClosed">
      <el-form
        ref="editLocationFormRef"
        :model="editLocationForm"
        label-width="100px"
        :rules="editLocationFormRules"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="editLocationForm.address1"
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editLocationForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editLocationDialog = false">取 消</el-button>
        <el-button type="primary" @click="editLocationDialog = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流进度对话框 -->
    <el-dialog title="查看物流进度" :visible.sync="processDialog" width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'

export default {
  data() {
    return {
      info: {
        query: '',
        pagenum: 1,
        pagesize: 8
      },
      cityData,
      orderList: [],
      total: 0,
      //   修改地址对话框状态
      editLocationDialog: false,
      //   物流进度对话框状态
      processDialog: false,
      //   修改收货地址的表单
      editLocationForm: {
        address1: [],
        address2: ''
      },
      editLocationFormRules: {
        address1: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      //保存物流信息
      progressInfo: []
    }
  },
  created() {
    this.getOrderlist()
  },
  methods: {
    async getOrderlist() {
      const { data: res } = await this.$http.get('orders', {
        params: this.info
      })
      console.log(res)
      if (res.meta.status !== 200) return this.$msg('获取订单列表失败')

      this.total = res.data.total
      this.orderList = res.data.goods
      //   if (this.orderList[0].pay_status == '0') return (this.color = 'danger')
      //   else this.color = 'success'
    },
    // 页号发现时候
    handleSizeChange(newSize) {
      //   console.log(newSize)
      this.info.pagesize = newSize
      this.getOrderlist()
    },
    // 页码改变时候
    handleCurrentChange(newPage) {
      this.info.pagenum = newPage
      this.getOrderlist()
    },
    showLocationDialog() {
      this.editLocationDialog = true
    },
    dialogClosed() {
      this.$refs.editLocationFormRef.resetFields()
    },
    // 物流进度函数
    async showProcessDialog() {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      console.log(res)

      if (res.meta.status !== 200) return this.$msg('获取物流进度失败')
      this.progressInfo = res.data
      //显示对话框
      this.processDialog = true
    }
  }
  //   computed: {
  //     type() {
  //       if (this.orderList.pay_status == '0') return (this.color = 'danger')
  //       else return 'success'
  //     }
  //   }
}
</script>

<style lang="less" scoped>
.input-with-select {
  width: 340px;
  // padding: 0 px 0 0px;
  margin-bottom: 15px;
}
</style>